// 引入koa-router
const Router = require('koa2-router');
const router = new Router();

const { timeTrans, formatPrice } = require("../../utils");
const {
  getParts,
  addParts,
  updateParts,
  deleteParts,
  showParts,
  batchDelete
} = require("../../model/admin/parts");

/* parts增删改查 */

// 查找parts
router.get("/",async (ctx) => {
  // console.log(ctx.request.query);
  ctx.body = await getParts();
});

// 添加parts
router.post("/add",async (ctx) => {
  const parts_id = Number(ctx.request.body.parts_id);
  // 排序值去重
  const data = await getParts();
  let isPartsId = data.some(item=>{
    return item.parts_id === parts_id;
  })
  if (isPartsId){
    ctx.body = {
      code: 500,
      msg: "排序值重复，请重新输入"
    };
  }else {
    const title = ctx.request.body.title;
    const price = formatPrice(ctx.request.body.price);
    const src = "http://localhost/images/home/" + ctx.request.body.src.name;
    const url = "#/product?id=" + ctx.request.body.product_id;
    const atime = timeTrans(ctx.request.body.atime);

    // console.log(ctx.request);
    // console.log(goods_id,title,price,src,url,category,atime);
    // ctx.body = 'ok';

    try {
      await addParts([parts_id,title,price,src,url,atime]);
      ctx.body = {
        code: 200,
        msg: "添加成功",
      };
    }catch (err) {
      ctx.body = {
        code: 500,
        msg: "添加失败",
        err,
      };
    }

  }
});

// 修改parts
router.post("/update",async (ctx) => {
  const parts_id = Number(ctx.request.body.parts_id);
  const title = ctx.request.body.title;
  const price = formatPrice(ctx.request.body.price);
  const src = "http://localhost/images/home/" + ctx.request.body.src.name;
  const url = "#/product?id=" + ctx.request.body.product_id;
  const atime = timeTrans(ctx.request.body.atime);

  try {
    await updateParts([title,price,src,url,atime,parts_id]);
    ctx.body = {
      code: 200,
      msg: "修改成功",
    };
  }catch (err) {
    ctx.body = {
      code: 500,
      msg: "修改失败",
      err,
    };
  }
});

// 删除单个parts
router.get("/delete",async (ctx) => {
  // console.log(req.query);
  const parts_id = Number(ctx.request.query.parts_id);

  try {
    await deleteParts([parts_id]);
    ctx.body = {
      code: 200,
      msg: "删除成功",
    };
  }catch (err) {
    ctx.body = {
      code: 500,
      msg: "删除失败",
      err,
    };
  }
});

// 是否展示parts
router.get("/show", (ctx) => {
  // console.log(req.query);
  const isShow = ctx.request.query.isShow !== "false";
  const parts_id = Number(ctx.request.query.parts_id);

  try {
    showParts([isShow, parts_id]);
    ctx.body = {
      code: 200,
      msg: "修改成功",
    };
  }catch (err) {
    ctx.body = {
      code: 500,
      msg: "修改失败",
      err,
    };
  }
});

// 批量删除
router.post("/batchDelete",async (ctx) => {
  const arr = ctx.request.body.arr;
  arr.forEach((item,index,arr)=>{
    arr[index] = Number(item);
  })
  console.log(arr);
  // ctx.body = 'ok'
  try {
    await batchDelete([arr]);
    ctx.body = {
      code:200,
      msg:"删除成功"
    };
  }catch (err) {
    // console.log(err);
    ctx.body = {
      code:500,
      msg:"删除失败",
      err
    };
  }

});

module.exports = router;
